\hypertarget{Math_8h}{
\section{Source/Math.h File Reference}
\label{Math_8h}\index{Source/Math.h@{Source/Math.h}}
}
Set of (hopefully) fast maths functions that may help when developing a game.  




This graph shows which files directly or indirectly include this file:\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=110pt]{Math_8h__dep__incl}
\end{center}
\end{figure}
\subsection*{Defines}
\begin{CompactItemize}
\item 
\#define \hyperlink{Math_8h_e2c6358b57c9746e0487142c91eb2872}{Math\_\-DegToRad}(D)~((D)$\ast$0.017453293)
\item 
\#define \hyperlink{Math_8h_d692d1d0d0e803e5d3af007dfb6e8c09}{Math\_\-RadToDeg}(R)~((R)$\ast$57.295779513)
\end{CompactItemize}
\subsection*{Functions}
\begin{CompactItemize}
\item 
int \hyperlink{Math_8h_c00c49af9a31978fe238beeff5c50e4b}{Math\_\-IsPrime} (int Number)
\item 
int \hyperlink{Math_8h_f6300d0bc0197908efdf0a670f00fff9}{Math\_\-GetFirstNPrimes} (int Amount, int $\ast$Array)
\item 
int \hyperlink{Math_8h_bdefad6b2c1e53e13997af30c88da361}{Math\_\-GetFirstNPrimesLowMemory} (int Amount, int $\ast$Array)
\item 
int \hyperlink{Math_8h_e3bb31874b10d6db86b5f122a27e97d0}{Math\_\-GCD} (int Number0, int Number1)
\item 
int \hyperlink{Math_8h_a0c7630257961b0fbe9a67878fd0b264}{Math\_\-LCM} (int Number0, int Number1)
\item 
int \hyperlink{Math_8h_b749bbb9e867f6a3562fa045769f1c1b}{Math\_\-DigitalSum} (int Number)
\item 
int \hyperlink{Math_8h_e615ba7f6ed096f78703953b75df948a}{Math\_\-DigitalRoot} (int Number)
\item 
float \hyperlink{Math_8h_e2950fa96e6a20e77df248a0c4813773}{Math\_\-AngleX} (float Angle)
\item 
float \hyperlink{Math_8h_27a6ccc23290f75da56c9f04c0a73d8d}{Math\_\-AngleY} (float Angle)
\item 
float \hyperlink{Math_8h_c7750aa06cabb14a90221ba1b407400a}{Math\_\-XYAngle} (float X1, float Y1, float X2, float Y2)
\item 
float \hyperlink{Math_8h_ef2359c47b76d48f436fcf7f35c2b114}{Math\_\-NormaliseAngle} (float Angle)
\item 
int \hyperlink{Math_8h_45adb385b80fbdb23add7c75bacccb3b}{Math\_\-PrimeFactors} (int Number, int $\ast$$\ast$Factors)
\item 
int \hyperlink{Math_8h_ad68b226bf8af6482eb9827f5becb8cb}{Math\_\-PrimeFactorCount} (int Number)
\item 
int \hyperlink{Math_8h_315d4c30d0335235ca267b4215412699}{Math\_\-FactorCount} (int Number)
\item 
void \hyperlink{Math_8h_c48f70d4c8ade13146d49dab5f609615}{Math\_\-ClearCache} ()
\end{CompactItemize}


\subsection{Detailed Description}
Set of (hopefully) fast maths functions that may help when developing a game. 

\begin{Desc}
\item[Author:]Daniel White \end{Desc}
\begin{Desc}
\item[Version:]0.1 \end{Desc}
\hypertarget{Vector_8h_Description}{}\subsection{Description}\label{Vector_8h_Description}
Set of (hopefully) fast maths functions that may help when developing a game. Contains various functions to do with primes, factors, trigonometry etc. Aims to be very fast. 

Definition in file \hyperlink{Math_8h-source}{Math.h}.

\subsection{Define Documentation}
\hypertarget{Math_8h_e2c6358b57c9746e0487142c91eb2872}{
\index{Math.h@{Math.h}!Math\_\-DegToRad@{Math\_\-DegToRad}}
\index{Math\_\-DegToRad@{Math\_\-DegToRad}!Math.h@{Math.h}}
\subsubsection[{Math\_\-DegToRad}]{\setlength{\rightskip}{0pt plus 5cm}\#define Math\_\-DegToRad(D)~((D)$\ast$0.017453293)}}
\label{Math_8h_e2c6358b57c9746e0487142c91eb2872}


Macro for converting degrees into radians. 

Definition at line 16 of file Math.h.\hypertarget{Math_8h_d692d1d0d0e803e5d3af007dfb6e8c09}{
\index{Math.h@{Math.h}!Math\_\-RadToDeg@{Math\_\-RadToDeg}}
\index{Math\_\-RadToDeg@{Math\_\-RadToDeg}!Math.h@{Math.h}}
\subsubsection[{Math\_\-RadToDeg}]{\setlength{\rightskip}{0pt plus 5cm}\#define Math\_\-RadToDeg(R)~((R)$\ast$57.295779513)}}
\label{Math_8h_d692d1d0d0e803e5d3af007dfb6e8c09}


Macro for converting radians into degrees. 

Definition at line 19 of file Math.h.

\subsection{Function Documentation}
\hypertarget{Math_8h_e2950fa96e6a20e77df248a0c4813773}{
\index{Math.h@{Math.h}!Math\_\-AngleX@{Math\_\-AngleX}}
\index{Math\_\-AngleX@{Math\_\-AngleX}!Math.h@{Math.h}}
\subsubsection[{Math\_\-AngleX}]{\setlength{\rightskip}{0pt plus 5cm}float Math\_\-AngleX (float {\em Angle})\hspace{0.3cm}{\tt  \mbox{[}inline\mbox{]}}}}
\label{Math_8h_e2950fa96e6a20e77df248a0c4813773}


Takes an angle, and returns the displacement a particle would be displaced by in the X direction if its velocity was 1.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Angle}]The angle in degrees where 0 is North and 90 is East. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The X displacement. \end{Desc}


Definition at line 235 of file Math.c.

\begin{Code}\begin{verbatim}236 {
237     if(Angle < 90.0)
238         return sin((float)Math_DegToRad(Angle));
239     if(Angle < 180.0)
240         return cos((float)Math_DegToRad(Angle-90.0));
241     if(Angle < 270.0)
242         return -sin((float)Math_DegToRad(Angle-180.0));
243     return -cos((float)Math_DegToRad(Angle-270.0));
244 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_27a6ccc23290f75da56c9f04c0a73d8d}{
\index{Math.h@{Math.h}!Math\_\-AngleY@{Math\_\-AngleY}}
\index{Math\_\-AngleY@{Math\_\-AngleY}!Math.h@{Math.h}}
\subsubsection[{Math\_\-AngleY}]{\setlength{\rightskip}{0pt plus 5cm}float Math\_\-AngleY (float {\em Angle})\hspace{0.3cm}{\tt  \mbox{[}inline\mbox{]}}}}
\label{Math_8h_27a6ccc23290f75da56c9f04c0a73d8d}


Takes an angle, and returns the displacement a particle would be displaced by in the Y direction if its velocity was 1.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Angle}]The angle in degrees where 0 is North and 90 is East. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The Y displacement. \end{Desc}


Definition at line 224 of file Math.c.

\begin{Code}\begin{verbatim}225 {
226     if(Angle < 90.0)
227         return -cos((float)Math_DegToRad(Angle));
228     if(Angle < 180.0)
229         return sin((float)Math_DegToRad(Angle-90.0));
230     if(Angle < 270.0)
231         return cos((float)Math_DegToRad(Angle-180.0));
232     return -sin((float)Math_DegToRad(Angle-270.0));
233 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_c48f70d4c8ade13146d49dab5f609615}{
\index{Math.h@{Math.h}!Math\_\-ClearCache@{Math\_\-ClearCache}}
\index{Math\_\-ClearCache@{Math\_\-ClearCache}!Math.h@{Math.h}}
\subsubsection[{Math\_\-ClearCache}]{\setlength{\rightskip}{0pt plus 5cm}void Math\_\-ClearCache ()}}
\label{Math_8h_c48f70d4c8ade13146d49dab5f609615}


When primes are generated with the GetFirstNPrimes() function, a cache of the primes is kept (if there is enough memory). This speeds up calls to many of the functions. However if the memory is needed, this function will clear the cache, freeing the memory used. 

if (Math\_\-PrimeCache!=NULL) 

Definition at line 338 of file Math.c.

\begin{Code}\begin{verbatim}339 {
341         free(Math_PrimeCache);
342     Math_PrimeCache=NULL;
343     Math_PrimeCacheSize=0;
344 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_e615ba7f6ed096f78703953b75df948a}{
\index{Math.h@{Math.h}!Math\_\-DigitalRoot@{Math\_\-DigitalRoot}}
\index{Math\_\-DigitalRoot@{Math\_\-DigitalRoot}!Math.h@{Math.h}}
\subsubsection[{Math\_\-DigitalRoot}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-DigitalRoot (int {\em Number})}}
\label{Math_8h_e615ba7f6ed096f78703953b75df948a}


Calculates the digital root of a number (repeated digit sum until the result is a single digit number).

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number}]The number to find the digital root of. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The digital root. \end{Desc}


Definition at line 216 of file Math.c.

\begin{Code}\begin{verbatim}217 {
218     if (Number%9==0)
219         return 9;
220     else
221         return Number%9;
222 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_b749bbb9e867f6a3562fa045769f1c1b}{
\index{Math.h@{Math.h}!Math\_\-DigitalSum@{Math\_\-DigitalSum}}
\index{Math\_\-DigitalSum@{Math\_\-DigitalSum}!Math.h@{Math.h}}
\subsubsection[{Math\_\-DigitalSum}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-DigitalSum (int {\em Number})}}
\label{Math_8h_b749bbb9e867f6a3562fa045769f1c1b}


Calculates the sum of digits in the given number (assuming base 10).

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number}]The number whose digits will be added. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The sum of the digits. \end{Desc}


Definition at line 203 of file Math.c.

\begin{Code}\begin{verbatim}204 {
205     int Sum=0;
206     
207     while(Number>0)
208     {
209         Sum+=Number%10;
210         Number/=10;
211     }
212     
213     return Sum;
214 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_315d4c30d0335235ca267b4215412699}{
\index{Math.h@{Math.h}!Math\_\-FactorCount@{Math\_\-FactorCount}}
\index{Math\_\-FactorCount@{Math\_\-FactorCount}!Math.h@{Math.h}}
\subsubsection[{Math\_\-FactorCount}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-FactorCount (int {\em Number})}}
\label{Math_8h_315d4c30d0335235ca267b4215412699}


Returns the amount of factors a number has (whether they be composite or prime).

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number}]The number to count the factors for. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The amount of factors. \end{Desc}


Definition at line 312 of file Math.c.

\begin{Code}\begin{verbatim}313 {
314     int *PrimeFactors=NULL;
315     int PrimeAmount=Math_PrimeFactors(Number, &PrimeFactors);
316     if (PrimeAmount==-1)
317         return -1;
318     
319     int Amount=1, Last=0, Index, Count=1;
320     for(Index=0;Index<PrimeAmount;++Index)
321     {
322         if (PrimeFactors[Index]!=Last)
323         {
324             Amount*=Count;
325             
326             Last=PrimeFactors[Index];
327             Count=2;
328         }
329         else
330             ++Count;
331     }
332     Amount*=Count;
333     
334     free(PrimeFactors); 
335     return Amount;
336 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_e3bb31874b10d6db86b5f122a27e97d0}{
\index{Math.h@{Math.h}!Math\_\-GCD@{Math\_\-GCD}}
\index{Math\_\-GCD@{Math\_\-GCD}!Math.h@{Math.h}}
\subsubsection[{Math\_\-GCD}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-GCD (int {\em Number0}, \/  int {\em Number1})}}
\label{Math_8h_e3bb31874b10d6db86b5f122a27e97d0}


Computes the greatest common divisor (also known as the highest common factors) of two numbers. Uses the Euclidean algorithm to compute the GCD.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number0}]The first number. \item[{\em Number1}]The second number. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The GCD of Number0 and Number1. \end{Desc}


Definition at line 186 of file Math.c.

\begin{Code}\begin{verbatim}187 {
188     int Temp;
189     while(Number1!=0)
190     {
191         Temp=Number1;
192         Number1=Number0%Number1;
193         Number0=Temp;
194     }
195     return Number0;
196 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_f6300d0bc0197908efdf0a670f00fff9}{
\index{Math.h@{Math.h}!Math\_\-GetFirstNPrimes@{Math\_\-GetFirstNPrimes}}
\index{Math\_\-GetFirstNPrimes@{Math\_\-GetFirstNPrimes}!Math.h@{Math.h}}
\subsubsection[{Math\_\-GetFirstNPrimes}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-GetFirstNPrimes (int {\em Amount}, \/  int $\ast$ {\em Array})}}
\label{Math_8h_f6300d0bc0197908efdf0a670f00fff9}


Fills an array with the first N primes. Uses an optomized sieve of Eratosthenes to generate the primes. Note: If memory is an issue, Math\_\-GetFirstNPrimesLowMemory should be used instead.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Amount}]Amount of primes to generate. \item[{\em Array}]An array of ints where the primes will be stored. Note: It is the user's duty to ensure this is big enough. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 on success, -1 on failure. \end{Desc}


Definition at line 18 of file Math.c.

\begin{Code}\begin{verbatim}19 {
20     if (Amount<=Math_PrimeCacheSize)
21     {
22         memcpy(Array, Math_PrimeCache, Amount);
23         return 0;
24     }
25     
26     int UpdateCache, *TempPointer=realloc(Math_PrimeCache, (Amount+1)*sizeof(Math_PrimeCache));
27     
28     if (TempPointer==NULL)
29         UpdateCache=0;
30     else
31     {
32         UpdateCache=1;
33         Math_PrimeCacheSize=Amount;
34         Math_PrimeCache=TempPointer;
35     }
36     
37     /* Work out an upper bound for the Amount'th prime number */
38     double A=2, B=4;
39     while(A!=B)
40     {
41         if (((int)(B/log(B)))<Amount)
42         {
43             A=B;
44             B*=1.15;
45         }
46         if (((int)(B/log(B)))>Amount)
47             B*=0.85;
48         if (((int)(B/log(B)))==Amount)
49             A=B;
50     }
51     long Max=(long)A;
52     
53     /* Now sieve */
54     long X=3, X2, Y, AmountFound=0, SqrtMax=sqrt(Max);
55     
56     bool *Numbers=calloc(Max, sizeof(bool));
57     if (Numbers==NULL)
58         return 1;
59     
60     Array[0]=2;
61     if (UpdateCache)
62         Math_PrimeCache[0]=2;
63     
64     while(X<SqrtMax)
65     {
66         if (Numbers[X]!=1)
67         {
68             ++AmountFound;
69             if (UpdateCache)
70                 Math_PrimeCache[AmountFound]=X;
71             Array[AmountFound]=X;
72             
73             X2=X<<1;
74             for(Y=X;Y<Max;Y+=X2)
75                 Numbers[Y]=1;
76         }
77         X+=2;
78     }
79     
80     while(AmountFound<Amount)
81     {
82         if (Numbers[X]!=1)
83         {
84             ++AmountFound;
85             if (UpdateCache)
86                 Math_PrimeCache[AmountFound]=X;
87             Array[AmountFound]=X;
88         }
89         X+=2;
90     }
91     
92     free(Numbers);
93     
94     return 0;
95 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_bdefad6b2c1e53e13997af30c88da361}{
\index{Math.h@{Math.h}!Math\_\-GetFirstNPrimesLowMemory@{Math\_\-GetFirstNPrimesLowMemory}}
\index{Math\_\-GetFirstNPrimesLowMemory@{Math\_\-GetFirstNPrimesLowMemory}!Math.h@{Math.h}}
\subsubsection[{Math\_\-GetFirstNPrimesLowMemory}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-GetFirstNPrimesLowMemory (int {\em Amount}, \/  int $\ast$ {\em Array})}}
\label{Math_8h_bdefad6b2c1e53e13997af30c88da361}


Fills an array with the first N primes. Uses an optomized trail-division method to decide if numbers are prime or not. Note: If memory is not an issue, Math\_\-GetFirstNPrimes could be used instead (which is faster).

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Amount}]Amount of primes to generate. \item[{\em Array}]An array of ints where the primes will be stored. Note: It is the user's duty to ensure this is big enough. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 on success, -1 on failure. \end{Desc}


Definition at line 97 of file Math.c.

\begin{Code}\begin{verbatim}98 {   
99     if (Amount<=Math_PrimeCacheSize)
100     {
101         memcpy(Array, Math_PrimeCache, Amount);
102         return 0;
103     }
104     
105     Array[0]=2;
106     Array[1]=3;
107     int Number=5, Found=2, Divisor, SqrtNumber, IsPrime;
108     for(;Found<Amount;)
109     {
110         SqrtNumber=sqrt(Number);
111         IsPrime=1;
112         for(Divisor=3;Divisor<=SqrtNumber;Divisor+=2)
113         {
114             if (Number%Divisor==0)
115             {
116                 IsPrime=0;
117                 Divisor=SqrtNumber;
118             }
119         }
120         if (IsPrime)
121         {
122             Array[Found]=Number;
123             ++Found;
124         }
125         
126         Number+=2;
127         if (Found==Amount)
128             return 0;
129             
130         SqrtNumber=sqrt(Number);
131         IsPrime=1;
132         for(Divisor=3;Divisor<=SqrtNumber;Divisor+=2)
133         {
134             if (Number%Divisor==0)
135             {
136                 IsPrime=0;
137                 Divisor=SqrtNumber;
138             }
139         }
140         if (IsPrime)
141         {
142             Array[Found]=Number;
143             ++Found;
144         }
145             
146         Number+=4;
147     }
148     
149     return 0;
150 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_c00c49af9a31978fe238beeff5c50e4b}{
\index{Math.h@{Math.h}!Math\_\-IsPrime@{Math\_\-IsPrime}}
\index{Math\_\-IsPrime@{Math\_\-IsPrime}!Math.h@{Math.h}}
\subsubsection[{Math\_\-IsPrime}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-IsPrime (int {\em Number})}}
\label{Math_8h_c00c49af9a31978fe238beeff5c50e4b}


Decides if a given number is prime or not. Uses a small array of primes to check for factors, before resorting to an optomized trail-division algorithm if needed.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number}]The number to check for primality. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]1 if the number is prime, 0 otherwise. \end{Desc}


Definition at line 152 of file Math.c.

\begin{Code}\begin{verbatim}153 {
154     int Amount=100;
155     int TempArray[Amount];
156     if (Math_PrimeCacheSize<Amount)
157         Math_GetFirstNPrimes(Amount, TempArray);
158     
159     /* Divide by primes */
160     int PrimeIndex;
161     for(PrimeIndex=0;PrimeIndex<Math_PrimeCacheSize;++PrimeIndex)
162     {
163         if (Number==Math_PrimeCache[PrimeIndex])
164             return 1;
165         else
166         {
167             while (Number%Math_PrimeCache[PrimeIndex]==0)
168                 Number/=Math_PrimeCache[PrimeIndex];
169         }
170     }
171     
172     if (Number==1)
173         return 0;
174     
175     /* Now try trial division */
176     int SqrtNumber=sqrt(Number), Divisor;
177     for(Divisor=3;Divisor<=SqrtNumber;Divisor+=2)
178     {
179         if (Number%Divisor==0)
180             return 0;
181     }
182     
183     return 1;
184 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_a0c7630257961b0fbe9a67878fd0b264}{
\index{Math.h@{Math.h}!Math\_\-LCM@{Math\_\-LCM}}
\index{Math\_\-LCM@{Math\_\-LCM}!Math.h@{Math.h}}
\subsubsection[{Math\_\-LCM}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-LCM (int {\em Number0}, \/  int {\em Number1})\hspace{0.3cm}{\tt  \mbox{[}inline\mbox{]}}}}
\label{Math_8h_a0c7630257961b0fbe9a67878fd0b264}


Computes the least common multiple (also known as the lowest common multiple) of two numbers.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number0}]The first number. \item[{\em Number1}]The second number. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The LCM of Number0 and Number1. \end{Desc}


Definition at line 198 of file Math.c.

\begin{Code}\begin{verbatim}199 {
200     return abs(Number0*Number1)/Math_GCD(Number0, Number1);
201 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_ef2359c47b76d48f436fcf7f35c2b114}{
\index{Math.h@{Math.h}!Math\_\-NormaliseAngle@{Math\_\-NormaliseAngle}}
\index{Math\_\-NormaliseAngle@{Math\_\-NormaliseAngle}!Math.h@{Math.h}}
\subsubsection[{Math\_\-NormaliseAngle}]{\setlength{\rightskip}{0pt plus 5cm}float Math\_\-NormaliseAngle (float {\em Angle})}}
\label{Math_8h_ef2359c47b76d48f436fcf7f35c2b114}


Normalises an angle, i.e. adds/subtracts multiples of 360 so that it is in the range 0$<$=Angle$<$360.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Angle}]The angle to normalise. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The normalised angle. \end{Desc}


Definition at line 251 of file Math.c.

\begin{Code}\begin{verbatim}252 {
253     while(Angle >= 360.0)
254         Angle -= 360.0;
255     while(Angle < 0.0)
256         Angle += 360.0;
257     return Angle;
258 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_ad68b226bf8af6482eb9827f5becb8cb}{
\index{Math.h@{Math.h}!Math\_\-PrimeFactorCount@{Math\_\-PrimeFactorCount}}
\index{Math\_\-PrimeFactorCount@{Math\_\-PrimeFactorCount}!Math.h@{Math.h}}
\subsubsection[{Math\_\-PrimeFactorCount}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-PrimeFactorCount (int {\em Number})}}
\label{Math_8h_ad68b226bf8af6482eb9827f5becb8cb}


Returns the amount of prime factors a number has.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number}]The number to count the factors for. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The amount of prime factors. \end{Desc}


Definition at line 304 of file Math.c.

\begin{Code}\begin{verbatim}305 {
306     int *PrimeFactors=NULL;
307     int PrimeAmount=Math_PrimeFactors(Number, &PrimeFactors);
308     free(PrimeFactors); 
309     return PrimeAmount;
310 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_45adb385b80fbdb23add7c75bacccb3b}{
\index{Math.h@{Math.h}!Math\_\-PrimeFactors@{Math\_\-PrimeFactors}}
\index{Math\_\-PrimeFactors@{Math\_\-PrimeFactors}!Math.h@{Math.h}}
\subsubsection[{Math\_\-PrimeFactors}]{\setlength{\rightskip}{0pt plus 5cm}int Math\_\-PrimeFactors (int {\em Number}, \/  int $\ast$$\ast$ {\em Factors})}}
\label{Math_8h_45adb385b80fbdb23add7c75bacccb3b}


Factorises a number into its prime factors.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em Number}]The number to factorise \item[{\em Factors}]A pointer to an array of ints. This must be passed by reference because the function may need to realloc it to hold the factors, hence the array location might change. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]On success the amount of factors is returned ($>$0), on failure -1 is returned. \end{Desc}


Definition at line 260 of file Math.c.

\begin{Code}\begin{verbatim}261 {
262     int TempPrimes[10];
263     Math_GetFirstNPrimes(10, TempPrimes);
264     
265     int FactorCount=0, PrimeIndex, *TempPointer;
266     
267     for(PrimeIndex=0;PrimeIndex<Math_PrimeCacheSize;++PrimeIndex)
268     {
269         while (Number%Math_PrimeCache[PrimeIndex]==0)
270         {
271             Number/=Math_PrimeCache[PrimeIndex];
272             ++FactorCount;
273             
274             TempPointer=realloc((*Factors), FactorCount*sizeof(*Factors));
275             if (TempPointer==NULL)
276                 return -1;
277             (*Factors)=TempPointer;
278             (*Factors)[FactorCount-1]=Math_PrimeCache[PrimeIndex];
279         }
280     }
281     
282     int Divisor;
283     for(Divisor=3;Divisor<=Number;Divisor+=2)
284     {
285         while (Number%Divisor==0)
286         {
287             Number/=Divisor;
288             ++FactorCount;
289             
290             TempPointer=realloc((*Factors), FactorCount*sizeof(*Factors));
291             if (TempPointer==NULL)
292                 return -1;
293             (*Factors)=TempPointer;
294             (*Factors)[FactorCount-1]=Divisor;          
295         }
296         
297         if (Number==1)
298             return FactorCount;
299     }
300     
301     return FactorCount;
302 }
\end{verbatim}
\end{Code}


\hypertarget{Math_8h_c7750aa06cabb14a90221ba1b407400a}{
\index{Math.h@{Math.h}!Math\_\-XYAngle@{Math\_\-XYAngle}}
\index{Math\_\-XYAngle@{Math\_\-XYAngle}!Math.h@{Math.h}}
\subsubsection[{Math\_\-XYAngle}]{\setlength{\rightskip}{0pt plus 5cm}float Math\_\-XYAngle (float {\em X1}, \/  float {\em Y1}, \/  float {\em X2}, \/  float {\em Y2})\hspace{0.3cm}{\tt  \mbox{[}inline\mbox{]}}}}
\label{Math_8h_c7750aa06cabb14a90221ba1b407400a}


Calculates the angle to get from the start point (X1, Y1) to the end point (X2, Y2).

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em X1}]X co-ordinate of the start point. \item[{\em Y1}]Y co-ordinate of the start point. \item[{\em X2}]X co-ordinate of the end point. \item[{\em Y2}]Y co-ordinate of the end point. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The angle. \end{Desc}


Definition at line 246 of file Math.c.

\begin{Code}\begin{verbatim}247 {
248     return Math_RadToDeg(atan2(Y2-Y1, X2-X1))+90;
249 }
\end{verbatim}
\end{Code}


